/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.view.backingbean;

import br.com.model.core.AdminDao;
import br.com.model.entity.Administrador;
import java.io.Serializable;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import org.primefaces.context.RequestContext;

@SessionScoped
@ManagedBean
public class BackingAdm implements Serializable {

    private Administrador a1 = new Administrador();
    private AdminDao aD1;
    private List<Administrador> admins;
    private DataModel listaAdmins;

    public BackingAdm() {
    }

    public DataModel getListarCliente() {
        List<Administrador> lista = aD1.list(Administrador.class);
        listaAdmins = new ListDataModel(lista);
        return listaAdmins;
    }

    public Administrador getA1() {
        return a1;
    }

    public void setA1(Administrador a1) {
        this.a1 = a1;
    }

    public List<Administrador> getAdmins() {
        return admins;
    }

    public String addCliente() {
        aD1.save(a1);
        return Outcomes.INDEX;
    }

    public String deleteCliente() {
        Administrador aTemp = (Administrador) (listaAdmins.getRowData());
        aD1.delete(aTemp);
        return Outcomes.MENU;
    }

    public String alteraCliente() {
        aD1.update(a1);
        return Outcomes.MENU;
    }

    public String logar(ActionEvent actionEvent) {
        if ("adm".equals(a1.getLogin()) && ("adm").equals(a1.getPassword())) {
            a1.setLogin("");
            a1.setPassword("");
            return "admin";
        } else {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage("Login ou Senha Incorretos!"));
            a1.setLogin("");
            a1.setPassword("");
            return "main";
        }

    }

    public void login(ActionEvent actionEvent) {
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;
        boolean loggedIn = false;

        if (a1.getLogin() != null && a1.getLogin().equals("admin") && a1.getPassword() != null && a1.getPassword().equals("admin")) {
            loggedIn = true;
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", a1.getNomeAdmin());
        } else {
            loggedIn = false;
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Inválido", "Senha ou Login Incorretos");
        }

        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("loggedIn", loggedIn);
    }
}
